package mn.slide.laborberlin.main;

import mn.slide.laborberlin.tabgroup.GroupStarter;
import mn.slide.laborberlin.tabgroup.HomeGroupActivity;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class AllgemeineActivity extends Activity implements OnClickListener
{
	private ListView	listV;
	private String[]	mStrings	= {};
	private TextView	call1 , call2 , call3;
	EditText			search_edt;
	@Override
	protected void onCreate( Bundle savedInstanceState )
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);
		listV = (ListView) findViewById(R.id.main_list);
		View footerView = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.allgemeine, null, false);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings);
		listV.addFooterView(footerView);
		listV.setAdapter(adapter);
		call1 = (TextView) footerView.findViewById(R.id.call1);
		call1.setOnClickListener(this);
		call2 = (TextView) footerView.findViewById(R.id.call2);
		call2.setOnClickListener(this);
		call3 = (TextView) footerView.findViewById(R.id.call3);
		call3.setOnClickListener(this);
		search_edt = (EditText) findViewById(R.id.search_edt);
		search_edt.setOnKeyListener(new OnKeyListener()
		{
			public boolean onKey( View v , int keyCode , KeyEvent event )
			{
				if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER))
				{
					String k = search_edt.getText().toString();
					if (k.length() > 0)
					{
						EditText searchbox = (EditText) v;
						InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
						imm.hideSoftInputFromWindow(searchbox.getWindowToken(), 0);
						Intent intent = new Intent(AllgemeineActivity.this, SearchActivity.class);
						intent.putExtra("searchTxt", k);
						HomeGroupActivity.group.goTo(intent);

					}
					return true;
				}
				return false;
			}
		});
	}

	@Override
	public void onClick( View v )
	{
		if (call1 == v)
		{
			callNumber("030-450 56 9069");
		}
		if (call2 == v)
		{
			callNumber("030-130 14 3939");
		}
		if (call3 == v)
		{
			callNumber("030-450 56 7800");
		}
	}

	@Override
	public void onBackPressed()
	{

		GroupStarter.getInstance().onBackpressed(this);
	}

	private void callNumber( final String title )
	{
		AlertDialog.Builder dialog = new AlertDialog.Builder(getParent());
		dialog.setTitle(title);
		dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
		{

			@Override
			public void onClick( DialogInterface dialog , int which )
			{
				dialog.dismiss();
			}

		});
		dialog.setPositiveButton("Call", new DialogInterface.OnClickListener()
		{

			@Override
			public void onClick( DialogInterface dialog , int which )
			{
				try
				{
					Intent intent = new Intent(Intent.ACTION_CALL);
					intent.setData(Uri.parse("tel:" + title));
					startActivity(intent);
				}
				catch (Exception e)
				{
					Log.e("SampleApp", "Failed to invoke call", e);
				}
			}

		});
		dialog.show();
	}
}
